package link.anyauto.weclass.util;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.List;

import link.anyauto.weclass.R;
import link.anyauto.weclass.adapter.OneTypeItemSource;
import link.anyauto.weclass.adapter.OneTypeItemSourceListAdapter;
import link.anyauto.weclass.adapter.OneTypeItemSourcePagerAdapter;
import link.anyauto.weclass.databinding.DemoTabItemBinding;
import link.anyauto.weclass.tab.TabItem;


/**
 * Created by discotek on 17-1-24.
 */

public class BindUtil {
    @BindingAdapter("oneTypeSource")
    public static void setOneTypeSourceListView(ListView view, OneTypeItemSource source) {
        if(source == null)
            return;
        OneTypeItemSourceListAdapter adapter = new OneTypeItemSourceListAdapter(source, LayoutInflater.from(view.getContext()));
        view.setAdapter(adapter);
    }

    @BindingAdapter("oneTypeSource")
    public static void setOneTypeSourceViewPager(ViewPager view, OneTypeItemSource source) {
        if(source == null)
            return;
        OneTypeItemSourcePagerAdapter adapter = new OneTypeItemSourcePagerAdapter(source, LayoutInflater.from(view.getContext()));
        view.setAdapter(adapter);
    }

    @BindingAdapter("emptyView")
    public static void setEmptyView(ListView view, int resId) {
        view.setEmptyView(((View) view.getParent()).findViewById(resId));
    }

    @BindingAdapter("imgUri")
    public static void setImgUri(ImageView view, Uri uri) {
        try {
            view.setImageURI(uri);
        } catch (OutOfMemoryError error) {
            ToastUtil.toast(R.string.pic_too_large);
        }
    }

    @BindingAdapter("tabs")
    public static void setTabs(LinearLayout layout, List<TabItem> tabs) {
        layout.removeAllViews();
        if(tabs != null && !tabs.isEmpty()) {
            LayoutInflater inflater = LayoutInflater.from(layout.getContext());
            for (TabItem item : tabs) {
                DemoTabItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.demo_tab_item, layout, false);
                binding.setItem(item);
                layout.addView(binding.getRoot());
            }
        }
    }

    @BindingAdapter({"tabCount", "tabSelected"})
    public static void setTabCount(final LinearLayout layout, final int count, final int selected) {
        if(count <= 0)
            return;

        layout.post(new Runnable() {
            @Override
            public void run() {
                int w = layout.getWidth();
                View bg = layout.getChildAt(0);
                ViewGroup.LayoutParams lp = bg.getLayoutParams();
                lp.width = w / count;
                bg.setLayoutParams(lp);
                bg.setX(selected * lp.width);
            }
        });
    }

    @BindingAdapter("pageSelected")
    public static void setPageSelected(ViewPager pager, int selected) {
        pager.setCurrentItem(selected, true);
    }

    @BindingAdapter("pos")
    public static void setViewPos(LinearLayout layout, float pos) {
        View view = layout.getChildAt(0);
        view.setX(view.getWidth() * pos);
    }

    @BindingAdapter("pageChange")
    public static void setPageChangeListener(ViewPager pager, ViewPager.OnPageChangeListener listener) {
        if(listener == null)
            return;
        pager.addOnPageChangeListener(listener);
    }

    @BindingAdapter("findAndBindAppBar")
    public static void setFindAndBindAppBar(ViewGroup viewGroup, Object anything) {
        Context ctx = viewGroup.getContext();
        Toolbar bar = (Toolbar) viewGroup.findViewById(R.id.toolbar);
        if(ctx != null && ctx instanceof AppCompatActivity && bar != null) {
            ((AppCompatActivity) ctx).setSupportActionBar(bar);
        }
    }

    @BindingAdapter("navIconListener")
    public static void setNavIconListener(Toolbar toolbar, View.OnClickListener listener) {
        toolbar.setNavigationOnClickListener(listener);
    }
}
